package com.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.entity.Gunfail;
import com.entity.Unitdoor;
import com.entity.Uploadfile;
import com.entity.Visitors;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.service.GunfailService;
import com.service.UnitdoorService;
import com.service.UploadService;
import com.service.VisitorsService;

@Controller
@RequestMapping("/v2")
public class GunController {
	@Resource
	UnitdoorService unitdoorService;
	@Resource
	UploadService uploadService;
	@Resource
	VisitorsService visitorsService;
	@Resource
	GunfailService gunfailService;
	
	@RequestMapping(value="/wwluserinfo",produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String wwluserinfo(@RequestBody String json,ModelMap map){
		//当前系统时间为结束时间
		Date data=new Date();
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String stoptime = dateFormat.format(data);
        
		JsonParser parser = new JsonParser(); 				 // 创建JSON解析器
        JsonObject object = (JsonObject) parser.parse(json); // 创建JsonObject对象
        
		String starttime=object.get("starttime").getAsString();
        String idnumber=object.get("idnumber").getAsString();
        
        
		Unitdoor unitdoor=unitdoorService.selectIdnumber(idnumber);
		Map<String, Object> maps=new HashMap<String, Object>();
		maps.put("starttime",starttime);
		maps.put("stoptime", stoptime);
		maps.put("yaddress", unitdoor.getAddress());
		List<Uploadfile> list=uploadService.selectTimeAddress(maps);
		List<Visitors>   list1=visitorsService.selectTimeAndAddress(maps);
		List<Uploadfile> listfail=uploadService.gunfail("0");
		List<Visitors> listfail1=visitorsService.gunfail("1");
		map.addAttribute("list", list);
		map.addAttribute("list1", list1);
		map.addAttribute("listfail", listfail);
		map.addAttribute("listfail1", listfail1);
		map.addAttribute("systemtime", stoptime);
		new JSONObject();
		JSONObject json1=JSONObject.fromObject(map);
		gunfailService.delete(idnumber);
		return json1.toString();
	}
	
	@RequestMapping(value="/wwlfail",produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String wwlfail(@RequestBody String json,Gunfail gunfail){
		JsonParser parser = new JsonParser(); 				 // 创建JSON解析器
        JsonObject object = (JsonObject) parser.parse(json); // 创建JsonObject对象
		String id=object.get("id").getAsString();
        String gun=object.get("gun").getAsString();
        String usertype=object.get("usertype").getAsString();
        int id1=Integer.parseInt(id);
        gunfail.setId(id1);
        gunfail.setGun(gun);
        gunfail.setUsertype(usertype);
        int num=gunfailService.insert(gunfail);
        if(num==1){
        	return "{state"+":1}";	//成功
        }else{
        	return "{state"+":0}";	//失败
        }
	}
}
